/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.app.income.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.app.income.domain.IncomeInfo;
import me.zhengjie.modules.app.income.service.IncomeInfoService;
import me.zhengjie.modules.app.income.service.dto.IncomeInfoQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.format.annotation.DateTimeFormat;

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author Haojie Li
* @date 2021-03-02
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "应用：收益信息")
@RequestMapping("/api/incomeInfo")
public class IncomeInfoController {

    private final IncomeInfoService incomeInfoService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('incomeInfo:list')")
    public void download(HttpServletResponse response, IncomeInfoQueryCriteria criteria) throws IOException {
        incomeInfoService.download(incomeInfoService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询收益信息")
    @ApiOperation("查询收益信息")
    @PreAuthorize("@el.check('incomeInfo:list')")
    public ResponseEntity<Object> query(IncomeInfoQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(incomeInfoService.queryAll(criteria,pageable),HttpStatus.OK);
    }


    @GetMapping("allHotelIncome")
    @Log("查询所有的酒店收益信息")
    @ApiOperation("查询所有的酒店收益信息")
    @PreAuthorize("@el.check('incomeInfo:list')")
    public ResponseEntity<Object> queryAllHotelIncome(IncomeInfoQueryCriteria criteria, Pageable pageable){

        return new ResponseEntity<>(incomeInfoService.findIncomeBetweenDate(criteria,pageable),HttpStatus.OK);
    }
    @GetMapping("allHotelIncome/download")
    @Log("查询所有的酒店收益信息")
    @ApiOperation("查询所有的酒店收益信息")
    @PreAuthorize("@el.check('incomeInfo:list')")
    public void queryAllHotelIncomeDownload(HttpServletResponse response,IncomeInfoQueryCriteria criteria) throws IOException{

        incomeInfoService.downloadCustomer(incomeInfoService.findIncomeBetweenDateAll(criteria),response);
//        incomeInfoService.download(incomeInfoService.queryAll(criteria), response);
    }

    @PostMapping("exportBenefitDetails")
    @Log("根据酒店ID查询酒店导出收益详情")
    @ApiOperation("导出所有的酒店收益信息")
    @PreAuthorize("@el.check('incomeInfo:list')")
    public void exportBenefitDetails(HttpServletResponse response,@RequestBody IncomeInfoQueryCriteria criteria)throws IOException{
        incomeInfoService.exportBenefitDetails(criteria,response);
    }

    @GetMapping("incomeDetail")
    @Log("根据酒店ID查询酒店收益详情")
    @ApiOperation("查询所有的酒店收益信息")
    @PreAuthorize("@el.check('incomeInfo:list')")
    public ResponseEntity<Object> queryHotelIncomeByHotelId(IncomeInfoQueryCriteria criteria, Pageable pageable){

    	return new ResponseEntity<>(incomeInfoService.findInComeByHotelId(criteria,pageable),HttpStatus.OK);
    }



    @GetMapping("incomeDetail/download")
    @Log("根据酒店ID查询酒店收益详情")
    @ApiOperation("查询所有的酒店收益信息")
    @PreAuthorize("@el.check('incomeInfo:list')")
    public void queryHotelIncomeByHotelIdDownload(HttpServletResponse response,IncomeInfoQueryCriteria criteria) throws IOException{

    	incomeInfoService.downloadFindInComeByHotelId(incomeInfoService.findInComeByHotelIdDownload(criteria),response);
    }

    @PostMapping
    @Log("新增收益信息")
    @ApiOperation("新增收益信息")
    @PreAuthorize("@el.check('incomeInfo:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody IncomeInfo resources){
        return new ResponseEntity<>(incomeInfoService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改收益信息")
    @ApiOperation("修改收益信息")
    @PreAuthorize("@el.check('incomeInfo:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody IncomeInfo resources){
        incomeInfoService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PutMapping("updateIsClose")
    @Log("修改收益信息")
    @ApiOperation("修改收益信息")
    @PreAuthorize("@el.check('incomeInfo:edit')")
    public ResponseEntity<Object> updateIsClose(@Validated @RequestBody IncomeInfo resources){
        incomeInfoService.updateIsCloseByHotelId(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }



    @Log("删除收益信息")
    @ApiOperation("删除收益信息")
    @PreAuthorize("@el.check('incomeInfo:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody String[] ids) {
        incomeInfoService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
